package com.lw.common.customview.popup_bottom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.chad.library.adapter4.viewholder.QuickViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.lxj.xpopup.widget.CheckView;
import com.lxj.xpopup.widget.VerticalRecyclerView;

import java.util.List;
import java.util.Objects;

/**
 * Description: 底部的列表对话框
 */
@SuppressLint("ViewConstructor")
public class BottomListPopupView extends BottomPopupView {
    RecyclerView recyclerView;
    TextView tv_title, tv_cancel;
    View vv_divider;
    protected int bindLayoutId;
    protected int bindItemLayoutId;
    List<BaseBottomBean> list;

    /**
     *
     */
    public BottomListPopupView(@NonNull Context context, List<BaseBottomBean> list, OnSelectListener selectListener) {
        super(context);
        this.list = list;
        this.selectListener = selectListener;
        addInnerContent();
    }

    public BottomListPopupView(@NonNull Context context, String title, List<BaseBottomBean> list, OnSelectListener selectListener) {
        super(context);
        this.title = title;
        this.list = list;
        this.selectListener = selectListener;
        addInnerContent();
    }

    @Override
    protected int getImplLayoutId() {
        return bindLayoutId == 0 ? com.lxj.xpopup.R.layout._xpopup_bottom_impl_list : bindLayoutId;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate() {
        super.onCreate();
        recyclerView = findViewById(com.lxj.xpopup.R.id.recyclerView);
        if (bindLayoutId != 0) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        }
        tv_title = findViewById(com.lxj.xpopup.R.id.tv_title);
        tv_cancel = findViewById(com.lxj.xpopup.R.id.tv_cancel);
        vv_divider = findViewById(com.lxj.xpopup.R.id.vv_divider);
        if (tv_cancel != null) {
            tv_cancel.setOnClickListener(v -> dismiss());
        }

        if (tv_title != null) {
            if (TextUtils.isEmpty(title)) {
                tv_title.setVisibility(GONE);
                if (findViewById(com.lxj.xpopup.R.id.xpopup_divider) != null)
                    findViewById(com.lxj.xpopup.R.id.xpopup_divider).setVisibility(GONE);
            } else {
                tv_title.setText(title);
            }
        }
        BaseQuickAdapter<BaseBottomBean, QuickViewHolder> adapter = new BaseQuickAdapter<>() {
            @NonNull
            @Override
            protected QuickViewHolder onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
                return new QuickViewHolder(bindItemLayoutId == 0 ? com.lxj.xpopup.R.layout._xpopup_adapter_text_match : bindItemLayoutId, viewGroup);
            }

            @Override
            protected void onBindViewHolder(@NonNull QuickViewHolder holder, int position, @Nullable BaseBottomBean item) {
                holder.setText(com.lxj.xpopup.R.id.tv_text, item.getName());

                if (bindItemLayoutId == 0) {
                    if (popupInfo.isDarkTheme) {
                        holder.<TextView>getView(com.lxj.xpopup.R.id.tv_text).setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_white_color));
                    } else {
                        holder.<TextView>getView(com.lxj.xpopup.R.id.tv_text).setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_dark_color));
                    }
                }
                // 对勾View
                if (!Objects.equals(checkedCode, "")) {
                    if (holder.getViewOrNull(com.lxj.xpopup.R.id.check_view) != null) {
                        holder.getView(com.lxj.xpopup.R.id.check_view).setVisibility(item.getId().equals(checkedCode) ? VISIBLE : GONE);
                        holder.<CheckView>getView(com.lxj.xpopup.R.id.check_view).setColor(XPopup.getPrimaryColor());
                    }
                    holder.<TextView>getView(com.lxj.xpopup.R.id.tv_text).setTextColor(item.getId().equals(checkedCode) ?
                            XPopup.getPrimaryColor() : ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_title_color));
                    holder.<TextView>getView(com.lxj.xpopup.R.id.tv_text).setGravity(XPopupUtils.isLayoutRtl(getContext()) ? Gravity.END : Gravity.START);
                } else {
                    if (holder.getViewOrNull(com.lxj.xpopup.R.id.check_view) != null)
                        holder.getView(com.lxj.xpopup.R.id.check_view).setVisibility(GONE);
                    holder.<TextView>getView(com.lxj.xpopup.R.id.tv_text).setGravity(Gravity.CENTER);
                }
            }
        };
        adapter.submitList(list);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            if (selectListener != null) {
                selectListener.onSelect(position, list.get(position));
            }
            if (!Objects.equals(checkedCode, "")) {
                checkedCode = list.get(position).getId();
                adapter.notifyDataSetChanged();
            }
            if (popupInfo.autoDismiss) dismiss();
        });

        recyclerView.setAdapter(adapter);
        applyTheme();
    }

    CharSequence title;

    String checkedCode = "";

    /**
     * 设置默认选中的位置
     */
    public BottomListPopupView setCheckedPosition(String code) {
        this.checkedCode = code;
        return this;
    }

    protected void applyTheme() {
        if (bindLayoutId == 0) {
            if (popupInfo.isDarkTheme) {
                applyDarkTheme();
            } else {
                applyLightTheme();
            }
        }
    }

    @Override
    protected void applyDarkTheme() {
        super.applyDarkTheme();
        ((VerticalRecyclerView) recyclerView).setupDivider(true);
        tv_title.setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_white_color));
        if (tv_cancel != null)
            tv_cancel.setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_white_color));
        findViewById(com.lxj.xpopup.R.id.xpopup_divider).setBackgroundColor(
                ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_list_dark_divider)
        );
        if (vv_divider != null) vv_divider.setBackgroundColor(Color.parseColor("#1B1B1B"));
        getPopupImplView().setBackground(XPopupUtils.createDrawable(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_dark_color),
                popupInfo.borderRadius, popupInfo.borderRadius, 0, 0));
    }

    @Override
    protected void applyLightTheme() {
        super.applyLightTheme();
        ((VerticalRecyclerView) recyclerView).setupDivider(false);
        tv_title.setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_dark_color));
        if (tv_cancel != null)
            tv_cancel.setTextColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_dark_color));
        findViewById(com.lxj.xpopup.R.id.xpopup_divider).setBackgroundColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_list_divider));
        if (vv_divider != null)
            vv_divider.setBackgroundColor(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_white_color));
        getPopupImplView().setBackground(XPopupUtils.createDrawable(ContextCompat.getColor(getContext(), com.lxj.xpopup.R.color._xpopup_light_color),
                popupInfo.borderRadius, popupInfo.borderRadius, 0, 0));
    }

    private OnSelectListener selectListener;

    public BottomListPopupView setOnSelectListener(OnSelectListener selectListener) {
        this.selectListener = selectListener;
        return this;
    }

    public interface OnSelectListener {
        void onSelect(int position, BaseBottomBean item);
    }
}

